package camid.util.dataanalysis;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DowntimeLogAnalyser {

	public static final String DOWNTIME_STRING = "09:48:53.574 [TG-750-2000 24-31] INFO  camid.distribution.broker.Requestor - Downtime (camid.test.testobjects.DelayObject/delay4680/doDelay) => 99";
//	public static final String DOWNTIME_REGEX = "\\d{2}\\x3a\\d{2}\\x3a\\d{2}\\x2e\\d{3} \\x5bTG-(\\d{2,3})-(\\d{3,4})";
	public static final String DOWNTIME_REGEX = ".+TG\\x2d(\\d+)\\x2d(\\d+)\\s.+INFO.+Downtime.+\\x3d\\x3e (\\d+)";
//			"\\s\\x5bTG\\x2d(\\d{3})\\x2d(\\d{3}).+\\5d";
//			"\\sINFO.+Downtime.+\\x3d\\x3e\\s(\\d+)"; 
	
	public static final Pattern DT_PATTERN = Pattern.compile(DOWNTIME_REGEX);
	
	private Map<String, DataMetric> dtMap;
	
	public DowntimeLogAnalyser()
	{
		Comparator<String> comp = new Comparator<String>(){

			@Override
			public int compare(String arg0, String arg1) {
				String[] arg0a = arg0.split("#");
				String[] arg1a = arg1.split("#");
				Integer n0 = Integer.parseInt(arg0a[0]);
				Integer t0 = Integer.parseInt(arg0a[1]);
				Integer n1 = Integer.parseInt(arg1a[0]);
				Integer t1 = Integer.parseInt(arg1a[1]);
				if(n0 != n1)
					return n0 - n1;
				else
				{
					return t0 - t1;
				}
				
			}
			
		};
		dtMap = new TreeMap<String,DataMetric> ();
	}
	
	public void analyseStream(InputStream is)
	{
		Scanner scanner = new Scanner(is);
		while(scanner.hasNext())
		{
			String line = scanner.nextLine();
//			if(line.matches(DOWNTIME_REGEX))
//			{
//				System.out.println(line);
//			}
			this.analyseLine(line);
		}
	}
	
	public void analyseLine(String line)
	{
		Matcher m = DT_PATTERN.matcher(line);
		if(m.find())
		{
//			System.out.println(line);
//			for(int i = 0 ; i <= m.groupCount(); i++)
//			{
//				System.out.println(m.group(i));
//			}
//			System.out.println();
			Integer n = Integer.parseInt(m.group(1));
			Integer t = Integer.parseInt(m.group(2));
			Integer d = Integer.parseInt(m.group(3));
			if(n == 750 && t == 1000)
			{
				
			}
			String key = DataMetric.getKey(n, t);
//			System.out.println(key + " " + d);
			
//			System.out.println(key);
			DataMetric da = dtMap.get(key);
			if(da == null)
			{
				System.out.println(key);
				da = new DataMetric(n, t);
				dtMap.put(key, da);
			}
			da.getDowntimes().add(d.doubleValue());
			
		}
	}
	
	public void toStream(OutputStream os)
	{
		PrintWriter writer = new PrintWriter(os, true);
		for(Entry<String,DataMetric> entry : dtMap.entrySet())
		{
//			System.out.println(entry.getKey());
			DataMetric da = entry.getValue();
			for(Double d : da.getDowntimes())
			{
				writer.printf("%d;%d;%f\n", da.getNumberObjects(), da.getServiceTime(), d);
			}
		}
	}
	
	public void avgsToStream(OutputStream os) {
		PrintWriter writer = new PrintWriter(os, true);
		for(Entry<String,DataMetric> entry : dtMap.entrySet())
		{
			DataMetric da = entry.getValue();
			da.evaluate();
			writer.printf("%d;%d;%f;%f\n", da.getNumberObjects(), da.getServiceTime(), da.getAverage(), da.getStddev());
		}
	}
	
	public static void main(String[] args) throws IOException {
		
//		DowntimeArray da = new DowntimeArray(numberObjects, serviceTime)
//		Pattern pattern = Pattern.compile(DOWNTIME_REGEX);
//		Matcher m = pattern.matcher(DOWNTIME_STRING);
		
//		if(m.find())
//		{
//			System.out.println("Found something!");
//			Integer n = Integer.parseInt(m.group(1));
//			Integer t = Integer.parseInt(m.group(2));
//			Integer d = Integer.parseInt(m.group(3));
//			System.out.println(n);
//			System.out.println(t);
//			System.out.println(d);
//		}
		
		String[] experiments = new String[]{"migration","nomigration","replication","noreplication"};
		for(String exp : experiments)
		{
			DowntimeLogAnalyser analyser = new DowntimeLogAnalyser();
			
			
			
			Scanner scanner = new Scanner( new FileInputStream("/home/diego/development/workspace-middleware/camid3/results/14-03-2013_02-33_aws_migration/jmeter-sdout.log"));
			while(scanner.hasNext())
			{
				String line = scanner.nextLine();
//				if(line.matches(DOWNTIME_REGEX))
//				{
//					System.out.println(line);
//				}
				analyser.analyseLine(line);
			}
//			analyser.analyseLine(DOWNTIME_STRING);
			
			FileOutputStream fos = new FileOutputStream("downtime-delay-migration.csv");
			analyser.toStream(System.out);
			fos.close();
			
			fos = new FileOutputStream("downtime-delay-migration-avgs.csv");
			analyser.avgsToStream(System.out);
			fos.close();
		}
		
		
	}

	
	
}
